//
//  NSArray+log.m
//  XLProject
//
//  Created by xinglian on 2021/12/24.
//

#import "NSArray+log.h"
@implementation NSArray (log)
@dynamic sum;
@dynamic avg;
@dynamic max;
@dynamic min;
- (CGFloat)sum
{
    return [[self valueForKeyPath:@"@sum.floatValue"] floatValue];
}
- (CGFloat)avg
{
    return [[self valueForKeyPath:@"@avg.floatValue"] floatValue];
}
- (CGFloat)max
{
    return [[self valueForKeyPath:@"@max.floatValue"] floatValue];
}
- (CGFloat)min
{
    return [[self valueForKeyPath:@"@min.floatValue"] floatValue];
}
- (NSString *)description
{
    NSMutableString *strM = [NSMutableString stringWithString:@"(\n"];
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[NSString class]])
        {
            [strM appendFormat:@"\t\t%@,\n", obj];
        }
        else
        {
            [strM appendFormat:@"\t\t%@,\n", [obj description]];
        }
    }];
    [strM appendString:@"\t\t)"];
    return strM;
}

- (NSArray *)charactersOrder
{
    //方法1，耗费时间少，速度快
    NSSortDescriptor * sort = [NSSortDescriptor sortDescriptorWithKey:@"pinyin" ascending:YES];
    NSArray *array = [NSArray arrayWithObject:sort];
    return [self sortedArrayUsingDescriptors:array];
    
    //方法2，转换成pinyin耗费时间久，速度慢
    /*
     NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch|NSNumericSearch|NSWidthInsensitiveSearch|NSForcedOrderingSearch;
     NSComparator sort = ^(NSString *obj1,NSString *obj2){
     NSString *pinyin1 = [obj1 pinyin];
     NSString *pinyin2 = [obj2 pinyin];
     NSRange range = NSMakeRange(0,pinyin1.length);
     return [pinyin1 compare:pinyin2 options:comparisonOptions range:range];
     };
     return [self sortedArrayUsingComparator:sort];
     */
}

/**
 *@pragma 按数组元素(字符串的首字母)进行分组
 */
- (NSArray *)orderWithFirstCharacter
{
    
    //
    //    NSArray *path =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    //    NSString *cachePath = path[0];
    //    NSLog(@"%@",cachePath);
    //    NSString *filePathName = [cachePath stringByAppendingPathComponent:@"agePlist.plist"];
    //    NSArray *dataArr = [NSMutableArray arrayWithContentsOfFile:filePathName];
    //    if([dataArr count]> 0)
    //    {
    //        return dataArr;
    //    }
    //
    
    NSMutableArray *copyArr = [NSMutableArray arrayWithArray:self];
    NSMutableArray *array = [NSMutableArray array];
    
    
    //方法1
    for (char ch='A'; ch<='Z'; ch++)
    {
        if (ch=='I' || ch=='U' || ch=='V')
            continue;
        //char->NSString
        NSString *chStr = [NSString stringWithFormat:@"%c",ch];
        NSMutableArray *subArr = [NSMutableArray array];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue:subArr forKey:chStr];
        [array addObject:dict];
    }
    
    [copyArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
     {
        NSString *string = [NSString stringWithFormat:@"%@",(NSString *)obj];
        ///获取首字母
        NSMutableString *source = [string mutableCopy];
        CFStringTransform((CFMutableStringRef)source, NULL, kCFStringTransformMandarinLatin, NO);
        CFStringTransform((CFMutableStringRef)source, NULL, kCFStringTransformStripDiacritics, NO);//这一行是去声调的
        NSString *firstChar = [source substringToIndex:1];//获取首字母
        if ([firstChar isEqualToString:@"a"])
        {
            [[array[0] valueForKey:@"A"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"b"])
        {
            [[array[1] valueForKey:@"B"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"c"])
        {
            [[array[2] valueForKey:@"C"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"d"])
        {
            [[array[3] valueForKey:@"D"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"e"])
        {
            [[array[4] valueForKey:@"E"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"f"])
        {
            [[array[5] valueForKey:@"F"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"g"])
        {
            [[array[6] valueForKey:@"G"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"h"])
        {
            [[array[7] valueForKey:@"H"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"j"])
        {
            [[array[8] valueForKey:@"J"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"k"])
        {
            [[array[9] valueForKey:@"K"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"l"])
        {
            [[array[10] valueForKey:@"L"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"m"])
        {
            [[array[11] valueForKey:@"M"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"n"])
        {
            [[array[12] valueForKey:@"N"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"o"])
        {
            [[array[13] valueForKey:@"O"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"p"])
        {
            [[array[14] valueForKey:@"P"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"q"])
        {
            [[array[15] valueForKey:@"Q"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"r"])
        {
            [[array[16] valueForKey:@"R"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"s"])
        {
            [[array[17] valueForKey:@"S"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"t"])
        {
            [[array[18] valueForKey:@"T"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"w"])
        {
            [[array[19] valueForKey:@"W"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"x"])
        {
            [[array[20] valueForKey:@"X"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"y"])
        {
            [[array[21] valueForKey:@"Y"] addObject:string];
        }
        else if ([firstChar isEqualToString:@"z"])
        {
            [[array[22] valueForKey:@"Z"] addObject:string];
        }
        
    }];
    
    for (NSMutableDictionary *dict in array.reverseObjectEnumerator)
    {
        if ([[[dict allValues] firstObject] count] < 1)
        {
            [array removeObject:dict];
        }
    }
    //    [array writeToFile:filePathName atomically:YES];
    
    return array;
    
    
    //方法2 耗时
    /*
     for (char ch='A'; ch<='Z'; ch++)
     {
     if (ch=='I' || ch=='O' || ch=='U' || ch=='V')
     continue;
     //首字母相同的数组
     NSMutableArray *subArr = [NSMutableArray array];
     //char->NSString
     NSString *chStr = [NSString stringWithFormat:@"%c",ch];
     //倒叙遍历
     
     [copyArr enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop)
     {
     NSString *string = [NSString stringWithFormat:@"%@",(NSString *)obj];
     NSString *firstChar = [[string pinyin] substringToIndex:1];//获取首字母
     NSString *upperFirstChar = [firstChar uppercaseString];//转换成大写字母
     if([upperFirstChar isEqualToString:chStr])
     {
     [subArr insertObject:string atIndex:0];
     [copyArr removeObject:string];
     }
     }];
     
     [array addObject:@{chStr:subArr}];
     
     }
     [array writeToFile:filePathName atomically:YES];
     return array;
     */
}

/**
 *@pragma 索引，获取A～Z的数组
 */
+ (NSArray *)upperCharacters
{
    NSMutableArray *array = [NSMutableArray array];
    for (char ch='A'; ch<='Z'; ch++) {
        if (ch=='I' || ch=='U' || ch=='V')
            continue;
        [array addObject:[NSString stringWithFormat:@"%c",ch]];
    }
    return array;
}
@end
